# -*-coding:UTF-8-*-
from flask import Flask
import subprocess,os,json
app=Flask(__name__)

@app.route("/")
def index():

    try:
        p = subprocess.Popen(['python3', 'sms.py'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        output, err = p.communicate()
        # print("短信内容:")
        # print(output.decode('utf-8'))
        phone_data=output.decode('utf-8')
    except subprocess.TimeoutExpired as e:
        return {"error":e.decode('utf-8')}

    try:
        p = subprocess.Popen(['python3', 'dcim.py'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        output, err = p.communicate()
        # print("短信内容:")
        # print(output.decode('utf-8'))
        phone_data=output.decode('utf-8')
    except subprocess.TimeoutExpired as e:
        return {"error":e.decode('utf-8')}

    try:
        p = subprocess.Popen(['python3', 'phone.py'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        output, err = p.communicate()
        # print("短信内容:")
        # print(output.decode('utf-8'))
        phone_data=output.decode('utf-8')
    except subprocess.TimeoutExpired as e:
        return {"error":e.decode('utf-8')}

    try:
        p = subprocess.Popen(['python3', 'sys.py'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        output, err = p.communicate()
        # print("短信内容:")
        # print(output.decode('utf-8'))
        phone_data=output.decode('utf-8')
    except subprocess.TimeoutExpired as e:
        return {"error":e.decode('utf-8')}

    try:
        p = subprocess.Popen(['python3', 'application.py'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        output, err = p.communicate()
        # print("短信内容:")
        # print(output.decode('utf-8'))
        phone_data=output.decode('utf-8')
    except subprocess.TimeoutExpired as e:
        return {"error":e.decode('utf-8')}

    return {"message":"successfull"}

@app.route("/status")
def status():
    try:
        p = subprocess.Popen(['adb', 'devices'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        output, err = p.communicate()
        # print("短信内容:")
        # print(output.decode('utf-8'))
        status_data=output.decode('utf-8')
    except subprocess.TimeoutExpired as e:
        return {"error":e.decode('utf-8')}
    status_data=status_data.replace("\n","")
    if status_data=="List of devices attached":
        status_data="no"
    else:
        status_data="yes"
    return {"message":status_data}

@app.route("/battery")
def battery():

    try:
        # if os.path.exists("screen/screen.png"):
        #     os.remove("screen/screen.png")
        p = subprocess.Popen(['adb', 'shell', 'dumpsys', 'battery', '|', 'grep','level'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        output, err = p.communicate()
    except subprocess.TimeoutExpired as e:
        return {"error":e.decode('utf-8')}
    output=output.decode('utf-8')

    try:
        # if os.path.exists("screen/screen.png"):
        #     os.remove("screen/screen.png")
        p = subprocess.Popen(['adb', 'shell', 'wm', 'size'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        size, err = p.communicate()
    except subprocess.TimeoutExpired as e:
        return {"error":e.decode('utf-8')}
    size=size.decode('utf-8')

    try:
        # if os.path.exists("screen/screen.png"):
        #     os.remove("screen/screen.png")
        p = subprocess.Popen(['adb', 'shell', 'cat', '/proc/meminfo','|','grep','MemTotal'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        memtotal, err = p.communicate()
    except subprocess.TimeoutExpired as e:
        return {"error":e.decode('utf-8')}
    memtotal=memtotal.decode('utf-8')

    try:
        p = subprocess.Popen(['adb', 'shell', 'cat', '/proc/cpuinfo','|','grep','Processor'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        cpuinfo, err = p.communicate()
    except subprocess.TimeoutExpired as e:
        return {"error":e.decode('utf-8')}
    cpuinfo=cpuinfo.decode('utf-8')

    try:
        p = subprocess.Popen(['adb', 'shell', 'cat', '/proc/stat','|','grep','btime'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        btime, err = p.communicate()
    except subprocess.TimeoutExpired as e:
        return {"error":e.decode('utf-8')}
    btime=btime.decode('utf-8')

    try:
        p = subprocess.Popen(['adb', 'shell', 'df', '-h','|','grep','/data/media/0'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        df, err = p.communicate()
    except subprocess.TimeoutExpired as e:
        return {"error":e.decode('utf-8')}
    df=df.decode('utf-8')

    try:
        p = subprocess.Popen(['adb', 'shell', 'dumpsys', 'activity','|','grep','mLastResumedActivity'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        activity, err = p.communicate()
    except subprocess.TimeoutExpired as e:
        return {"error":e.decode('utf-8')}
    activity=activity.decode('utf-8')

    try:
        p = subprocess.Popen(['adb', 'shell', 'top', '-n','1','|','grep','cpu'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        cpu, err = p.communicate()
    except subprocess.TimeoutExpired as e:
        return {"error":e.decode('utf-8')}
    cpu=cpu.decode('utf-8')

    try:
        p = subprocess.Popen(['adb', 'shell', 'top', '-n','1','|','grep','Mem'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        mem, err = p.communicate()
    except subprocess.TimeoutExpired as e:
        return {"error":e.decode('utf-8')}
    mem=mem.decode('utf-8')

    return {"data":output,"size":size,"memtotal":memtotal,"cpuinfo":cpuinfo,"btime":btime,"df":df,"activity":activity,"cpu":cpu,"mem":mem}

@app.route("/mount")
def mount():
    with open("config.json","r") as dirpath:
        dirpath=dirpath.read()
        dirpath=json.loads(dirpath)
        dirpaths=dirpath["dirpath"]

    try:
        p = subprocess.Popen(['mount', '--bind','/',dirpaths],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        output, err = p.communicate()
        dir_data=output.decode('utf-8')
    except subprocess.TimeoutExpired as e:
        pass
    return {"msg":"successfull"}

if __name__ == '__main__':
    app.run(host="0.0.0.0",port=8080,debug=True)
